package com.huawei.statistics.utils;

import com.huawei.statistics.common.service.wx.aes.XMLParse;
import org.apache.commons.codec.binary.Base64;
import org.springframework.web.util.HtmlUtils;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

/**
 * Created by XuZhansi on 2017/9/6.
 */
public class AESUtils {
    /**
     * 密钥算法
     */
    private static final String ALGORITHM = "AES";
    /**
     * 加解密算法/工作模式/填充方式
     */
    private static final String ALGORITHM_MODE_PADDING = "AES/ECB/PKCS5Padding";


    /**
     * AES加密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static String encryptData(String password, String data) throws Exception {
        // 创建密码器
        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        SecretKeySpec key = new SecretKeySpec(MD5Utils.getMD5Code(password).toLowerCase().getBytes(), ALGORITHM);
        // 初始化
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return Base64.encodeBase64String(cipher.doFinal(data.getBytes()));
    }

    /**
     * AES解密
     *
     * @param base64Data
     * @return
     * @throws Exception
     */
    public static String decryptData(String password, String base64Data) {
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
            SecretKeySpec key = new SecretKeySpec(MD5Utils.getMD5Code(password).toLowerCase().getBytes(), ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, key);
            return new String(cipher.doFinal(Base64.decodeBase64(base64Data)));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
        String xml="&lt;xml&gt;&lt;return_code&gt;SUCCESS&lt;/return_code&gt;&lt;appid&gt;&lt;![CDATA[wxd285a01924c19af5]]&gt;&lt;/appid&gt;&lt;mch_id&gt;&lt;![CDATA[1487488982]]&gt;&lt;/mch_id&gt;&lt;nonce_str&gt;&lt;![CDATA[35bc5e667822a486ed045afbbd747d99]]&gt;&lt;/nonce_str&gt;&lt;req_info&gt;&lt;![CDATA[+Iguy/bAOAjic0qpLGUSrmQnjoFPt/gxbvWnG4QndXfmhEfhb3IojTFohNQ+96k047CDk+9HXQwsdLY/Tz0e482OWTOQneyWyK39pkLXwhytKKL4d+9hkY7qjQX4T/azE+n1nq/Z93Uf73LY5NZm79Zr2qIVKalN/yIyqc/ZCH+6lqOENoKny0qKltldKLW35ahDaCEM5+fAhAD5iSMXL6JnvuJSEGl+NaA+QFrlCfv4ECzIIQ99W83slsq9IIjEuzH/T+hhy76ErhrHIloM0sEKkDeyya23YmFyeQp18IExZQ6f6Dc5mXmKuK1U7HxwA7xaISe6Z3F1TY6DNY2NPOWcgr/cMWxsArwI1xgws2rN9EWWXHlp0i5aYjyfonKFC56g1Coek5PJPQO2bdboJc9Mf7OJ9awSawY5NzMmrLIUSsCMVr5rtTKhExGttEXPGGhnhnZmhGMCyVGToEbTItGiHf/EuqHcJ3K3sK9vVzP7LGLZKPnHe9hj8NfduWtS5WTH4O2EcS3gmW382N26dy7G/xKE+TNAGTQYF/01I7RLve5IHL7rTjoUyODyuG94uvWBr4/uXi4LINPhsZ2iw8N00/Wda7KuklLSEegV274pmmlfPNtg7OmeP2v54Kuw1K6zZo0drlE6cptIOc6gQhrP7PdV6h/NMDS3hRmzCoSTjmw3GPuDI5RAL+wyBg0Xko3SsgA1c76ZrU9Cen+VfiyXMQq123CH18Ybe57MOvCjoz9t5/GpuQQv747AerPItWSAG7rqA9hHz5LxSG/dAx9pFH8C2TXtNefB5HUmFxbAKT4jKX7oTIdZezQFZhHm57DNzKw/chQFn2Ai31l6ouYc974LUw0aTczMDZgxWL5kXG4IWVVqIhKjzcc7fkG4TAfWkMKfBlxkJD+nhfxqN09TbscXieA22vRLojcMlIf1tcxDGPPMqLZ2Zn+xFXTDsIsiPkqJysRBlwBM2haGQPOUxRo3mMTFzwM3cU7erGDdYUXnCEu7/OEFVuPMKmPvnYTI2H7Vjgyt6VBgsseOj8XoQyQ286JK1JzlmX+r5XI=]]&gt;&lt;/req_info&gt;&lt;/xml&gt;";
        Map<String, String> map = XMLParse.xml2Map(HtmlUtils.htmlUnescape(xml));
        if (map.get("return_code").equals("SUCCESS")) {
            String encryMsg = map.get("req_info");
            System.out.println("加密内容："+encryMsg);
            String decryMsg=AESUtils.decryptData("SELxqHkkqcUlPl03avHvx6wURRxEPdHI",encryMsg);
            System.out.println("解密内容："+decryMsg);
        }
    }
}
